import axios from 'axios'
import { Toast } from 'zarm'

const MODE = import.meta.env.MODE

axios.defaults.baseURL = MODE == 'development' ? 'http://127.0.0.1:7001' : 'http://api.chennick.wang'
axios.defaults.withCredentials = true
axios.defaults.headers['X-Requested-with'] = 'XMLHttpRequest'
axios.defaults.headers['Authorization'] =  `${localStorage.getItem('token') || null}`
axios.defaults.headers.post['Content-Type'] = 'application/json'

axios.interceptors.response.use(res => {
    if (typeof res.data != 'object') {
        Toast.show('服务端异常!')
        return Promise.reject(res)
    }
    console.log(res.data)
    if (res.data.code != 200) {
        if (res.data.msg) Toast.show(res.data.msg)
        if (res.data.code == 401) {
            window.location.href = '/login'
        }

        return Promise.reject(res.data)
    }

    return res.data
})

export default axios